package com.bwie.cart.controller;

import com.bwie.cart.domain.dto.InsertCartDTO;
import com.bwie.cart.domain.vo.CartVO;
import com.bwie.cart.service.CartClientService;
import com.bwie.common.core.domain.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/client")
@RestController
@Log4j2
public class CartClientController {
    @Autowired
    private CartClientService cartService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 用户端
     * 获取购物车列表
     * @return
     */
    @GetMapping("/getCartList")
    public Result getCartList(){
        log.info("功能名称：获取购物车列表，请求url：{}，请求方式：{}",request.getRequestURI(),request.getMethod());
        List<CartVO> list = cartService.getCartList();
        Result<List<CartVO>> result = Result.success(list);
        log.info("功能名称：获取购物车列表，请求url：{}，请求方式：{}，响应结果：{}",request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 用户端
     * 添加购物车
     */
    @PostMapping("/insertCart")
    public Result insertCart(@RequestBody InsertCartDTO insertCartDTO){
        log.info("功能名称：添加购物车，请求url：{}，请求方式：{}，请求参数：{}",request.getRequestURI(),request.getMethod(),insertCartDTO);
        cartService.insertCart(insertCartDTO);
        Result<Object> result = Result.success();
        log.info("功能名称：添加购物车，请求url：{}，请求方式：{}，响应结果：{}",request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 删除购物车
     * @param skuId
     * @return
     */
    @DeleteMapping("/delCart/{skuId}")
    public Result delCart(@PathVariable Long skuId){
        log.info("功能名称：删除购物车，请求url：{}，请求方式：{}，请求参数：{}",request.getRequestURI(),request.getMethod(),skuId);
        cartService.delCart(skuId);
        Result<Object> result = Result.success();
        log.info("功能名称：删除购物车，请求url：{}，请求方式：{}，响应结果：{}",request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 修改购物车详情商品数量
     */
    @PutMapping("/updCartItemCount")
    public Result updCartItemCount(@RequestBody InsertCartDTO insertCartDTO)
    {
        log.info("功能名称：修改购物车详情商品数量，请求url：{}，请求方式：{}，请求参数：{}",request.getRequestURI(),request.getMethod(),insertCartDTO);
        cartService.updCartItemCount(insertCartDTO);
        Result<Object> result = Result.success();
        log.info("功能名称：修改购物车详情商品数量，请求url：{}，请求方式：{}，响应结果：{}",request.getRequestURI(),request.getMethod(),result);
        return result;
    }
}
